package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.CommonStreamUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.H5.ContestAllResultDto;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IBaseBasketballTeamService;
import com.ruoyi.system.service.IBaseContestBindingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static com.ruoyi.common.core.utils.PageUtils.startPage;


/**
 * 联赛Service业务层处理
 * 
 * @author wangbj
 * @date 2024-04-16
 */
@Slf4j
@Service
public class BaseContestBindingServiceImpl extends ServiceImpl<BaseContestBindingMapper, BaseContestBinding> implements IBaseContestBindingService
{

    @Resource
    private BaseContestMapper baseContestMapper;

    @Resource
    private BaseContestBindingGroupMapper baseContestBindingGroupMapper;
    @Resource
    private BasePlayerLogMapper basePlayerLogMapper;
    @Resource
    private IBaseBasketballTeamService iBaseBasketballTeamService;
    @Resource
    private BasketballPlayerAndTeamMapper basketballPlayerAndTeamMapper;
    @Resource
    private BaseTournamentMapper baseTournamentMapper;
    @Override
    public TableDataInfo selectList(BaseContest dto) {
        List<BaseContestBinding> contestBindingList = baseMapper.selectListById(dto.getId());
        if (CollUtil.isEmpty(contestBindingList)){
            return new TableDataInfo(new ArrayList<>(),0);
        }
        Set<Long> longs = CommonStreamUtil.transSet(contestBindingList, BaseContestBinding::getContestBindingId);
        longs.addAll( CommonStreamUtil.transSet(contestBindingList, BaseContestBinding::getContestId));
        longs.remove(dto.getId());
        startPage();
        List<BaseContest> baseContests = baseContestMapper.selectBatchIds(longs);
        baseContests = CommonStreamUtil.filter(baseContests, o -> Objects.equals(o.getIsDeleted(), 0L) &&
                Objects.equals(o.getAuditStatus(), 1));
        return new TableDataInfo(baseContests,contestBindingList.size());
    }

    @Override
    public List<BaseContestBindingGroup> getContestBindingGroupList(BaseContest dto) {
        AssertUtil.notNull(dto.getId(),"参数异常!");
        //联赛id 获取绑定联赛的组别列表id
        List<BaseContestBinding> baseContestBindings = this.baseMapper.selectByContestId(dto.getId());
        if (CollUtil.isEmpty(baseContestBindings)){
            return new ArrayList<>();
        }
        //根据id获取组别列表
        Set<Long> contestBindingIds = CommonStreamUtil.transSet(baseContestBindings, BaseContestBinding::getContestBindingId);
        List<BaseContestBinding> baseContestBindings1 = this.baseMapper.selectByBindingGroupIds(contestBindingIds);

        Set<Long> contestIds = CommonStreamUtil.transSet(baseContestBindings1, BaseContestBinding::getContestId);

        List<BaseContestBinding> baseContestBindings2 = this.baseMapper.selectByContestIds(contestIds);

        Set<Long> longs = CommonStreamUtil.transSet(baseContestBindings2, BaseContestBinding::getContestBindingId);
        return baseContestBindingGroupMapper.selectByIds(longs);
    }

    @Override
    public List<BaseContest> getContestListByBindingGroupId(Long id) {
        List<BaseContestBinding> baseContestBindings = this.baseMapper.selectByBindingGroupId(id);
        Set<Long> contestIds = CommonStreamUtil.transSet(baseContestBindings, BaseContestBinding::getContestId);
        if (CollUtil.isEmpty(contestIds)) {
            return new ArrayList<>();
        }
        return baseContestMapper.selectBatchIds(contestIds);
    }

    @Override
    public ContestAllResultDto contestAll(Long groupId) {
        List<BaseContestBinding> baseContestBindings = this.baseMapper.selectByBindingGroupId(groupId);
        if (CollUtil.isEmpty(baseContestBindings)) {
            return new ContestAllResultDto();
        }
        Set<Long> contestIds = CommonStreamUtil.transSet(baseContestBindings, BaseContestBinding::getContestId);
        ContestAllResultDto contestAllResultDto = basePlayerLogMapper.selectByContestIdsAll(contestIds);
        long count = iBaseBasketballTeamService.count(Wrappers.<BaseBasketballTeam>lambdaQuery()
                .in(BaseBasketballTeam::getLeagueId, contestIds)
                .eq(BaseBasketballTeam::getIsDeleted, 0L));
        contestAllResultDto.setTeamNum(count);
        long count1 = basketballPlayerAndTeamMapper.selectCount(Wrappers.<BasketballPlayerAndTeam>lambdaQuery()
                .in(BasketballPlayerAndTeam::getContestId, contestIds)
                .eq(BasketballPlayerAndTeam::getType, 0)
                .eq(BasketballPlayerAndTeam::getIsDeleted, 0L));
        contestAllResultDto.setPlayerNum(count1);

        List<BaseTournament> baseTournaments = baseTournamentMapper.selectByContestIdsBeginState(contestIds);
        contestAllResultDto.setViewNum(baseTournaments.stream().map(BaseTournament::getLiveNum).reduce(0L, Long::sum));
        return contestAllResultDto;
    }
}
